const express = require('express');
const router = express.Router();
const common = require('./common'); // 公共模块

// 数据库配置
const connection = common.mysqlConfig

router.post('/', (req, res, next) => {
  /* 
  params {
    title : // 标题
    content: // 内容
    attachment: // 附件
    sender: // 新增人名称
  }
  */
  let { title, content, attachment, sender } = req.body;
  // 获取员工 id
  let userID = req.headers.token;
  // 获取新增时间
  let date = common.DateFomart(new Date(), 'yyyy-mm-dd HH:mm');
  // 公会内容描述
  let preview = common.matchReg(content);
  console.log('\x1b[33m%s\x1b[0m', date);
  console.log('\x1b[33m%s\x1b[0m', '2019-12-14 12:22');
  // 新增 sql 语句
  const sql = `INSERT INTO MESSAGE_LIST (userID, title, content, attachment, sender, sendTime, preview) 
    VALUES ('${userID}', '${title}', '${content}', '${JSON.stringify(attachment)}', '${sender}', '${date}', '${preview}')`;
  console.log('\x1b[33m%s\x1b[0m', sql);
  connection.query(sql, (err, result) => {
    const data = common.resCommon1(err, result);
    res.json(data)
    // res.end('ddddd')
  });
});
     
module.exports = router;
